import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
   selector: 'sa-transport-algorithm',
   templateUrl: './transport-algorithm.component.html',
})
export class TransportAlgorithmComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	carList:any[] = [];
	companyId:string = '';
	companyList:any[] = [];
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	
	selectedInfo = {	//组件选择的信息
		'companyName':'',
		'companyId':'',
		'deviceId':'',
		'carNumber':'',
	}
	
	constructor(private jsonApiService: JsonApiService) {}

	ngOnInit() {
		this.initDate().then(()=>this.load());
	}
	//刷新
	refresh(){
		this.load();
	}
	getTime(date){
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var day = date.getDate()  < 10 ? "0" + date.getDate() : date.getDate();
		return year+"-"+month+"-"+day;
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = that.getTime(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = that.getTime(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						var date = new Date();
						$("#datetimepicker2").val(that.getTime(date));//当前时间
						date.setDate(date.getDate() - 7);//设置前一周
						$("#datetimepicker1").val(that.getTime(date));
						resolve();
					});
				}
			);
		})
	}
	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load() {
		this.startTime = $('#datetimepicker1').val();
		this.endTime = $('#datetimepicker2').val();
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}		
		let dataobj = {
				"pageSize":this.pageSize,
				"nowPage":this.curPage,
				"parameters":{
						"startTime":this.startTime,
						"endTime":this.endTime,
						"status":"4",
						'deviceId':this.selectedInfo.deviceId,
						'companyId':this.selectedInfo.companyId
					},
			}
		let reqdata='gridPager='+JSON.stringify(dataobj);
		this.jsonApiService.http("stats/transportforpage.html", reqdata).subscribe(data => {
			if(data&&data.exhibitDatas){
				for(let item of data.exhibitDatas){
					item.loadTime = common.secondToDate(item.loadTime);
					item.unloadTime = common.secondToDate(item.unloadTime);
					item.transportTime = common.secondToDate(item.transportTime);
					item.wbData = common.twoDecimal(item.wbData);
					item.percent = common.twoDecimal(item.percent);
					item.weight = common.twoDecimal(item.weight);
					item.maxSpeed = common.twoDecimal(item.maxSpeed);
					item.loadlng = common.twoDecimal(item.loadlng);
					item.loadlat = common.twoDecimal(item.loadlat);
					item.unloadlng = common.twoDecimal(item.unloadlng);
					item.unloadlat = common.twoDecimal(item.unloadlat);
					
				}
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	deleteRow(row){
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("stats/deletetransportdata.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
	//数据导出
	export_totle(){
		// if(this.startTime==''||this.endTime==''){
		// 	this.jsonApiService.message("起始结束时间不能为空");
		// 	return;
		// }
		// let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		// if(!timeCheck.status){
		// 	this.jsonApiService.message(timeCheck.message);
		// 	return;
		// }
		var type = {
			name:"运输过程统计(算法)-"+this.startTime+'至'+this.endTime
		}
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		var temdate = new Date();
		var nowTime = temdate.getTime().toString();
		var parameters = {
			startTime:this.startTime,
			endTime:this.endTime,
			fileName:type.name,
			deviceId:this.selectedInfo.deviceId,
			companyId:this.selectedInfo.companyId
		};	
		let timeCheck=common.correctTime(parameters, ['fileName']);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'stats/transportforExport.html')	
	}
	companySelected(val){
		this.selectedInfo = val;
	}
}
